//
//  MyFootView.swift
//  WangPu
//
//  Created by xiaozhi on 15/9/24.
//  Copyright (c) 2015年 zhixiao. All rights reserved.
//

import UIKit

class MyFootView: UIView {
    var buttonArray:NSMutableArray
    var section:NSInteger! = 0
    var btnClickHandle:((NSInteger,NSInteger)->Void)?
    var numberOfButton:NSInteger = 0{
        didSet{
            for oneView in subviews{
                if oneView is UIView{
                    oneView.removeFromSuperview()
                }
            }
            buttonArray.removeAllObjects()
            self.layoutShapeViews()
            self.layoutButtons()
            
        }
    }
    var titleArray:[String]?{
        didSet{
            var button:MyButton!
            for i in 0..<numberOfButton{
                button = buttonArray[i] as! MyButton
                if i < titleArray?.count{
                    button.setTitle(titleArray?[i], forState: UIControlState.Normal)
                }
            }
        }
    }
    var imageNomalArray:[String]?{
        didSet{
            var button:MyButton!
            for i in 0..<numberOfButton{
                button = buttonArray[i] as! MyButton
                if i < imageNomalArray?.count{
                     button.setImage(UIImage(named: imageNomalArray![i]), forState: UIControlState.Normal)
                }
            }
        }
    }
    var imageSelectArray:[String]?{
        didSet{
            var button:MyButton!
            for i in 0..<numberOfButton{
                button = buttonArray[i] as! MyButton
                if i < imageSelectArray?.count{
                    button.setImage(UIImage(named: imageSelectArray![i]), forState: UIControlState.Highlighted)
                }
            }
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    func  layoutButtons(){
        let buttonWidth = bounds.size.width/CGFloat(numberOfButton)
        let buttonHeight = bounds.size.height
        for i in 0..<numberOfButton{
            let button = MyButton(frame: CGRectMake(buttonWidth*CGFloat(i), 0, buttonWidth, buttonHeight))
            button.imageSize = CGSizeMake(15, 14)
            button.setTitle(titleArray?[i], forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(11)
            button.titleLabel?.textAlignment = NSTextAlignment.Center
//            button.setTitleColor(UIColor(red: 29.0/255, green: 173.0/255, blue: 248.0/255, alpha: 1.0), forState: UIControlState.Selected)
            button.setTitleColor(UIColor(red: 128.0/255, green: 128.0/255, blue: 128.0/255, alpha: 1.0), forState: UIControlState.Selected)

            button.setTitleColor(UIColor(red: 128.0/255, green: 128.0/255, blue: 128.0/255, alpha: 1.0), forState: UIControlState.Normal)
            button.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchDown)
            button.tag = i
            self.addSubview(button)
            buttonArray.addObject(button)
        }
    }
    func layoutShapeViews(){
        //横线
        let lineImageView = UIImageView(frame: CGRectMake(10.0,0.0, bounds.size.width-20, 1))
        lineImageView.backgroundColor =  UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
        self.addSubview(lineImageView)
        //纵向
        let buttonWidth = bounds.size.width/CGFloat(numberOfButton)
        var lineView:UIImageView!
        for i in 1..<numberOfButton{
            let start = ceil(buttonWidth*CGFloat(i))
            lineView = UIImageView(frame: CGRectMake(start, 8, 1, bounds.size.height-16))
            lineView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
            self.addSubview(lineView)
        }
    }
//    func setBrage(indexButton:NSInteger,brageNumber:NSInteger){
//        if indexButton >= buttonArray.count{
//            print("invalid para with indexButton")
//            return
//        }
//         let button = buttonArray[indexButton] as! TabButton
//          button.brageNumber = brageNumber
//    }
    func buttonClick(button:UIButton){
        btnClickHandle?(button.tag,section)
    }
    override init(frame: CGRect) {
        buttonArray = NSMutableArray()
        super.init(frame: frame)
        //        self.backgroundColor = UIColor(patternImage: UIImage(named: "tab-bg")!)
        backgroundColor = UIColor.whiteColor()
    }
    required init?(coder aDecoder: NSCoder) {
          buttonArray = NSMutableArray()
        super.init(coder: aDecoder)
    }
}
